package yxy.game.pm2.active.command.ghost;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actghost;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_神魔卡;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Item;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

/**
 * 神魔卡_抽将
 * @author Dell
 *
 */
@MessageType(proto = Command.ActGhostGachaInfo, classtype = ActiveMessage.ActGhostGachaInfoRequest.class)
public class CMD_Active_神魔卡_抽将_Info extends Command<ActiveMessage.ActGhostGachaInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_神魔卡_抽将_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActGhostGachaInfoRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActGhostGachaInfoResponse.Builder response = ActiveMessage.ActGhostGachaInfoResponse.newBuilder();
		
		ActiveModule_神魔卡 am = ActiveModule_神魔卡.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(am.getGhostmap().size() == 0) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		actghost ag = ActDataCenter.getData(101, actghost.class, playercode);
		Currency costcur = Currency.create(ag.getCosttype(), ag.getCostid(), ag.getCostnum());
		
		Currency item = Item.get(playercode, costcur.getType(), costcur.getDataId());
		long itemnum = item.getNumber();
		
		response.setItemnum(itemnum);
		response.setGachanum(am.getCknum());
		
		//抽将
		int[] indicias = ag.getIndicia();
		int[] idealtypes = ag.getIdealrewardtype();
		int[] idealids = ag.getIdealid();
		int[] idealnums = ag.getIdealid();
		for(int i = 0; i < indicias.length; i++) {
			ActiveMessage.HeartHero.Builder build = ActiveMessage.HeartHero.newBuilder();
			
			int indicia = indicias[i];
			int idealtype = idealtypes[i];
			int idealid = idealids[i];
			int idealnum = idealnums[i];
			
			Currency cur = Currency.create(idealtype, idealid, idealnum);
			PackageMessage.Item idealitem = new CurrencyHandle().packItem(cur);
			
			build.setSignid(indicia);
			build.setCur(idealitem);
			build.setSelect(am.getSelectid() == am.getSelectid());
			response.addHhs(build.build());
		}
		
//		HashMap<Integer, Currency> curmap = am.getCurmap();
//		for(int signid : curmap.keySet()) {
//			Currency cur = curmap.get(signid);
//			PackageMessage.Item signitem = new CurrencyHandle().packItem(cur);
//			ActiveMessage.HeartHero.Builder build = ActiveMessage.HeartHero.newBuilder();
//			build.setSignid(signid);
//			build.setCur(signitem);
//			build.setSelect(am.getSelectid() == signid);
//			response.addHhs(build.build());
//		}
		
		response.setSelectid(am.getSelectid());
		response.setStart(am.getTimeline().getStartTime().getMillis());
		response.setEnd(am.getTimeline().getEndTime().getMillis());
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActGhostGachaInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
